<?php
require_once 'classes/DB.php';
require_once 'config.php';

/**
 * This class represents a photo in the photoalbum system.
 */
class Photo
{	
	/**
	 * Creates an instance of the class.
	 */
	public function __construct(/*DB*/ $db) /*Photo*/
	{
	}
	
	private $photoId; /*int*/
	
	/**
	 * Gets the ID of the photo.
	 */
	public function getPhotoId() /*int*/
	{
		return $this->photoId;
	}
	
	/**
	 * Sets the ID of the photo.
	 *
	 * @param int $photoId
	 * 		  The new ID of the photo.
	 */
	public function setPhotoId(/*int*/ $photoId)
	{
		$this->photoId = $photoId;
	}
	
	private $creationTime; /*string*/
	
	/**
	 * Gets the creation time of the photo.
	 */
	public function getCreationTime() /*string*/
	{
		return $this->creationTime;
	}
	
	/**
	 * Sets the creation time of the photo.
	 *
	 * @param string $creationTIme
	 * 		  The new creation time of the photo.
	 */
	public function setCreationTime(/*string*/ $creationTime)
	{
		$this->creationTime = $creationTime;
	}
	
	private $name; /*string*/
	
	/**
	 * Gets the name of the photo.
	 */
	public function getName() /*string*/
	{
		return $this->name;
	}
	
	/**
	 * Sets the name of the photo.
	 *
	 * @param string $name
	 * 		  The new name of the photo.
	 */
	public function setName(/*string*/ $name)
	{
		$this->name = $name;
	}
	
	/**
	 * Gets the filename of the photo.
	 */
	public function getFilename() /*string*/
	{
		return FILES_DIR . $this->categoryId . FILE_DELIM . $this->photoId;
	}
	
	private $description; /*string*/
	
	/**
	 * Gets the description of the photo.
	 */
	public function getDescription() /*string*/
	{
		return $this->description;
	}
	
	/**
	 * Sets the description of the photo.
	 *
	 * @param string $description
	 * 		  The new description of the photo.
	 */
	public function setDescription(/*string*/ $description)
	{
		$this->description = $description;
	}
	
	private $categoryId; /*int*/
	
	/**
	 * Gets the ID of the category to which the photo belongs.
	 */
	public function getCategoryId() /*int*/
	{
		return $this->categoryId;
	}
	
	/**
	 * Sets the ID of the category to which the photo belongs.
	 *
	 * @param int $categoryId
	 * 		  The new ID of the category.
	 */
	public function setCategoryId(/*int*/ $categoryId)
	{
		$this->categoryId = $categoryId;
	}
	
	
	/**
	 * Loads a photo using its properties.
	 */
	public function load()
	{
		/*resource*/ $result = DB::getInstance()->executeProcedure('get_photo',
		array(DB::escapeInt($this->getPhotoId())),
				array('creation_date', 'name', 'description', 'category_id'));
		/*array*/ $photoData = DB::getInstance()->getNextRow($result);
		$this->setCreationTime($photoData['creation_date']);
		$this->setName($photoData['name']);
		$this->setDescription($photoData['description']);
		$this->setCategoryId($photoData['category_id']);
	}
	
	/**
	 * Edits the photo using its properties.
	 */
	public function edit()
	{
		if($this->getName() == "")
		{
			return "Photo name is empty";
		}
		$photos = Category::getPhotosByCategory($this->getCategoryId());
		foreach ($photos as $p)
		{
			if($p->getName() == $this->getName())
			{
				return "Photo with such name already exists";
			}
		}
		DB::getInstance()->executeProcedure('edit_photo',
				array(DB::escapeInt($this->getPhotoId()),
					  DB::escapeString($this->getName()),
					  DB::escapeString($this->getDescription())));
		return "";
	}
	
	/**
	 * Deletes the photo using its properties.
	 */
	public function delete()
	{
		DB::getInstance()->executeProcedure('delete_photo',
				array(DB::escapeInt($this->getPhotoId())));
		unlink(ROOT_DIR . DIR_DELIM . $this->getFilename());
	}
} // end class
?>